package com.web.hilover.controller;

import com.web.hilover.entity.User;
import com.web.hilover.service.UserService;
import com.web.hilover.util.CaptchaUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

@Controller
@Slf4j
public class AuthController {

    @Autowired
    private UserService userService;
    
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }
    
    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }
    
    @PostMapping("/api/register")
    @ResponseBody
    public Map<String, Object> register(@RequestParam String username,
                                      @RequestParam String password,
                                      @RequestParam String captcha,
                                      HttpSession session) {
        String realCaptcha = (String) session.getAttribute("captcha");
        if (!captcha.equalsIgnoreCase(realCaptcha)) {
            return Map.of("success", false, "message", "验证码错误");
        }
        
        try {
            User user = userService.register(username, password);
            return Map.of("success", true);
        } catch (Exception e) {
            return Map.of("success", false, "message", e.getMessage());
        }
    }
    
    @PostMapping("/api/login")
    @ResponseBody
    public Map<String, Object> login(@RequestParam String username,
                                   @RequestParam String password,
                                   @RequestParam String captcha,
                                   HttpSession session) {
        String realCaptcha = (String) session.getAttribute("captcha");
        if (!captcha.equalsIgnoreCase(realCaptcha)) {
            return Map.of("success", false, "message", "验证码错误");
        }
        
        try {
            User user = userService.login(username, password);
            session.setAttribute("loginUser", user);
            return Map.of("success", true);
        } catch (Exception e) {
            return Map.of("success", false, "message", e.getMessage());
        }
    }
    
    @GetMapping("/captcha")
    public void captcha(HttpServletResponse response, HttpSession session) throws IOException {
        String captcha = CaptchaUtil.generateCaptchaText();
        session.setAttribute("captcha", captcha);
        
        BufferedImage image = CaptchaUtil.createCaptchaImage(captcha);
        response.setContentType("image/jpeg");
        ImageIO.write(image, "jpg", response.getOutputStream());
    }
    
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "redirect:/";
    }
    

} 